﻿Public Class ADDUSREQ1
    Dim connect As CONDB = CONDB.NewConnection()
    Private Sub ADDUSERQ_Load(sender As Object, e As EventArgs) Handles Me.Load
        SelectDatagridUSER()
    End Sub
    Private Sub SelectDatagridUSER()
        Dim USOBJUSERER As New ADDUSER
        Dim MASUSER As DataTable
        MASUSER = connect.GetTable(USOBJUSERER.SelectMasuser_)
        USERGrid.PrimaryGrid.DataSource = MASUSER
    End Sub

    Private Sub AddUS_Click(sender As Object, e As EventArgs) Handles AddUS.Click
        If USERQTxt.Text.Length > 0 And TXTNAME.Text.Length > 0 And LASTNAME.Text.Length > 0 Then
            Dim objinsert As New ADDUSER
            objinsert.NAME_ = TXTNAME.Text
            objinsert.LASTNAME_ = LASTNAME.Text
            objinsert.POSITION_ = POSITION.Text
            objinsert.USREQ_ = USERQTxt.Text
            objinsert.USAPP_ = USREQ.Text
            objinsert.INSERTUSER()

            SelectDatagridUSER()

            USERIDTxt.Text = ""
            TXTNAME.Text = ""
            LASTNAME.Text = ""
            POSITION.Text = ""
            USERQTxt.Text = ""
        Else
            MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub EditUS_Click(sender As Object, e As EventArgs) Handles EditUS.Click
        If USERIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New ADDUSER
                objeditData.USERID_ = USERIDTxt.Text
                objeditData.NAME_ = TXTNAME.Text
                objeditData.LASTNAME_ = LASTNAME.Text
                objeditData.POSITION_ = POSITION.Text
                objeditData.USREQ_ = USERQTxt.Text

                objeditData.UPDATEUSER()

                SelectDatagridUSER()

                USERIDTxt.Text = ""
                TXTNAME.Text = ""
                LASTNAME.Text = ""
                POSITION.Text = ""
                USERQTxt.Text = ""
                SelectDatagridUSER()
                'cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddUS.Enabled = True
    End Sub

    Private Sub USERGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles USERGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            USERIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            USERIDTxt.Text = ""
        End If
        If grid.Cells("ชื่อพนักงาน").Value() IsNot DBNull.Value Then
            TXTNAME.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อพนักงาน").Value()
        Else
            TXTNAME.Text = ""
        End If
        If grid.Cells("นามสกุล").Value() IsNot DBNull.Value Then
            LASTNAME.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("นามสกุล").Value()
        Else
            LASTNAME.Text = ""
        End If
        If grid.Cells("ตำแหน่ง").Value() IsNot DBNull.Value Then
            POSITION.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำแหน่ง").Value()
        Else
            POSITION.Text = ""
        End If
        If grid.Cells("รหัสประจำตัว").Value() IsNot DBNull.Value Then
            USERQTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสประจำตัว").Value()
        Else
            USERQTxt.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE cpm.masuser SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `userid` = '" & USERIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddUS.Enabled = False
    End Sub

    Private Sub CLEAR_Click(sender As Object, e As EventArgs) Handles CLEAR.Click
        USERIDTxt.Text = ""
        TXTNAME.Text = ""
        LASTNAME.Text = ""
        POSITION.Text = ""
        USERQTxt.Text = ""
        SelectDatagridUSER()
    End Sub

    Private Sub txtUserReq_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUserReq.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            Dim dt As DataTable
            Dim sql As String
            If connect.GetTable("SELECT `usreq` FROM cpm.masuser WHERE `usreq` = '" & txtUserReq.Text & "' ").Rows.Count > 0 Then
                sql = "SELECT `userid`,`name`,`lastname`,`status`,`usreq` FROM cpm.masuser WHERE `usreq` = '" & txtUserReq.Text & "'; "
                dt = connect.GetTable(sql)
                If dt.Rows.Count > 0 Then
                    txtUsername.Text = dt.Rows(0)("name") & "  " & dt.Rows(0)("lastname")
                    USREQ.Text = dt.Rows(0)("userid")

                End If
                btnLogout.Visible = True
                'USERGrid.Enabled = True
                'GroupBox1.Enabled = True
            Else
                MsgBox("รหัสไม่ถูกต้อง", MsgBoxStyle.Critical)
                txtUsername.Text = ""
                txtUserReq.Text = ""
            End If
        End If
    End Sub

    Private Sub USREQ_TextChanged(sender As Object, e As EventArgs) Handles USREQ.TextChanged
        If USREQ.Text = 1 Then
            USERGrid.Enabled = True
            GroupBox1.Enabled = True
        Else
            MsgBox("คุณไม่มีสิทธี์เข้าใช้งาน", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub btnLogout_Click(sender As Object, e As EventArgs) Handles btnLogout.Click
        txtUserReq.Text = ""
        txtUsername.Text = ""
        USREQ.Text = ""
    End Sub
End Class